package com.kvest.mobile_newspaper.network;

import com.kvest.mobile_newspaper.core.Parser;
import com.kvest.mobile_newspaper.network.base.HTTPGetRequest;
import com.kvest.mobile_newspaper.network.base.HTTPRequestResult;

import android.util.Pair;

import java.util.ArrayList;
import java.util.List;

public class GetClubsListRequest 
{
	private int userId;
	
	private HTTPGetRequest request; 
	private GetClubsListResult resultListener;
	
	public GetClubsListRequest(int userId, GetClubsListResult resultListener)
    {
        this.userId = userId;
        this.resultListener = resultListener;
    }
	
	public void sendRequest()
    {
		request = new HTTPGetRequest(Urls.GET_CLUBS_LIST_URL + "?" + Urls.USER_ID_PARAM + "=" + 
                					 Integer.toString(userId));
		request.setResultListener(new HTTPRequestResult() 
		{	
			public void httResponce(int code, String result) 
			{
				List<Pair<String, String> > resultList = new ArrayList<Pair<String, String> >();
				if (code == HTTPRequestResult.RESULT_OK)
                {
					Parser parser = new Parser();
					resultListener.clubsListResult(parser.parseClubsList(result.trim(), resultList) ?
													GetClubsListResult.OK_RESULT : GetClubsListResult.ERROR_RESULT, 
													resultList);
                }
				else if (code == HTTPRequestResult.RESULT_ERROR)
                {
                    resultListener.clubsListResult(GetClubsListResult.ERROR_RESULT, resultList);
                }
				
			}
		});
		request.start();
    }
	
	public void interrupt()
    {
    	if (request != null)
    	{
    		request.interrupt();
    	}
    }
	
	public interface GetClubsListResult
    {
        public static final int OK_RESULT = 0;
        public static final int ERROR_RESULT = 1;

        public abstract void clubsListResult(int resultType, List<Pair<String, String> > clubs);
    }
}
